<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="分站">
              <a-input v-if="userInfo.userType == 2 || userInfo.userType == 3" disabled v-model="userInfoSite.name"></a-input>
              <a-input v-if="userInfo.userType == 1" placeholder="请输入分站名称" v-model="queryParam.siteName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="商家">
              <a-input v-if="userInfo.userType == 3" disabled v-model="userInfoMerchant.name"></a-input>
              <a-input v-if="userInfo.userType == 1 || userInfo.userType == 2" placeholder="请输入商家名称" v-model="queryParam.merchantName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="促销名称">
              <a-input placeholder="请输入促销名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="促销类型">
                  <j-dict-select-tag placeholder="请选择促销类型" v-model="queryParam.type" dictCode="shop_promo_type"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="状态">
                  <j-dict-select-tag placeholder="请选择状态" v-model="queryParam.status" dictCode="shop_promo_status"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="客户身份">
                  <j-dict-select-tag placeholder="请选择客户身份" v-model="queryParam.customer" dictCode="shop_promo_customer"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="优惠模式">
                  <j-dict-select-tag placeholder="请选择优惠模式" v-model="queryParam.model" dictCode="shop_promo_model"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="是否叠加">
                  <j-dict-select-tag placeholder="请选择是否叠加" v-model="queryParam.isSole" dictCode="shop_promo_isSole"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="创建时间">
                <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="queryParam.createDateFrom"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.createDateTo"></j-date>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-if="permissionData.shopPromoAdd" @click="handleAddCheck" type="primary" icon="plus">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <span slot="status" slot-scope="text, record">
          <span v-if="permissionData.shopPromoStatus">
            <a-popconfirm :title="statusTagData[text].popconfirmTitle" @confirm="() => handleStatusChange(record.id)">
              <a-tag class="mouse-hand" :color="statusTagData[text].color">{{statusTagData[text].text}}</a-tag>
            </a-popconfirm>
          </span>
          <span v-else>
            <a-tag :color="statusTagData[text].color">{{statusTagData[text].text}}</a-tag>
          </span>
        </span>

        <span slot="action" slot-scope="text, record">
          <span v-if="permissionData.shopPromoEdit && permissionData.shopPromoDelete">
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a-dropdown>
              <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a @click="handleDetail(record)">详情</a>
                </a-menu-item>
                <a-menu-item>
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </span>
          <span v-else>
            <span v-if="permissionData.shopPromoEdit">
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical" />
            </span>
            <span v-else-if="permissionData.shopPromoDelete">
              <a @click="handleDelete(record)">删除</a>
              <a-divider type="vertical" />
            </span>
            <a @click="handleDetail(record)">详情</a>
          </span>
        </span>

      </a-table>
    </div>

    <shop-promo-modal ref="modalForm" :fullscreen="false" @ok="modalFormOk"></shop-promo-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ShopPromoModal from './modules/ShopPromoModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import { upDownAction } from '@/api/manage'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ShopPromoList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      ShopPromoModal
    },
    data () {
      return {
        description: '商品促销信息管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'促销名称',
            align:"center",
            dataIndex: 'name'
          },
          {
            title:'促销类型',
            align:"center",
            dataIndex: 'type_dictText'
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' },
          },
          {
            title:'客户身份',
            align:"center",
            dataIndex: 'customer_dictText'
          },
          {
            title:'优惠模式',
            align:"center",
            dataIndex: 'model_dictText'
          },
          {
            title:'是否叠加',
            align:"center",
            dataIndex: 'isSole_dictText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/shopPromo/list",
          delete: "/shopPromo/delete",
          deleteBatch: "/shopPromo/deleteBatch",
          statusChange: "/shopPromo/statusChange",
        },
        dictOptions:{},
        statusTagData: {
          0: {
            color:"green",
            text: "开启",
            popconfirmTitle:"确定关闭吗?"
          }, 1: {
            color:"pink",
            text: "关闭",
            popconfirmTitle:"确定开启吗?"
          }
        }
      }
    },
    created() {
    },
    computed: {
      ...mapGetters([
        "permissionData", 
        "userInfo", 
        "userInfoSite",
        "userInfoMerchant",
        "userInfoCurrentShop"
      ]),
    },
    methods: {
      handleAddCheck () {
        // 点击添加按钮前校验是否有店铺信息
        if (!this.userInfoCurrentShop) {
          console.log('没有店铺信息')
          this.$message.warning('请选择门店后再新增商品促销')
        } else {
          this.handleAdd()
        }
      },
      handleStatusChange(id) {
        var that = this;
        that.loading = true;
        upDownAction(that.url.statusChange, {id}).then((res) => {
          if (res.success) {
            //重新计算分页问题
            that.reCalculatePage(that.selectedRowKeys.length)
            that.$message.success(res.message);
            that.loadData();
            that.onClearSelected();
          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.loading = false;
        });
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>